layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
      elem: '#startTime'
        	,value:new Date()
    });

    laydate.render({
      elem: '#endTime'
        	,value:new Date()
    });

    /**
     * 调度单表管理
     */
    var TDispatch = {
        tableId: "tDispatchTable"
    };
    
    /**
     * 初始化表格的列
     */
    TDispatch.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'xmmc', title: '项目名称', style:'font-weight: 600;'},
            {field: 'dispatchNumber', title: '运输单号', style:'font-weight: 600;',width: 180},
            {field: 'zhuangtai', title: '状态', style:'font-weight: 600;',
            	templet: function(d){
            		var str = "";
            		if (d.zhuangtai == '运输中'){
                    	str += '<span style="color:#ffaa00;font-weight: bold;">' + d.zhuangtai + '</span>'
					} else if (d.zhuangtai == '已完成'){
                    	str += '<span style="color:#19be6b;font-weight: bold;">' + d.zhuangtai + '</span>'
					} else {
                    	str += '<span style="font-weight: bold;">' + d.zhuangtai + '</span>'
					}
            		return str
            	}
			},
            {field: 'customerOrder', title: '客户单号', style:'font-weight: 600;'},
            {field: 'setMentiontime', title: '开单日期', style:'font-weight: 600;'},
            {field: 'startDate', title: '发车日期', style:'font-weight: 600;'},
            {field: 'pcsx', title: '派车时效', style:'font-weight: 600;'},

            {field: 'xianglu', title: '线路', style:'font-weight: 600;'},
            {field: 'getUnit', title: '收货客户', style:'font-weight: 600;'},

            {field: 'carnumber', title: '车牌号', style:'font-weight: 600;'},
            {field: 'driver1', title: '司机姓名', style:'font-weight: 600;'},
            {field: 'privatecarType', title: '车型', style:'font-weight: 600;'},
            {field: 'tooDeployVolume', title: '体积', style:'font-weight: 600;'},
            {field: 'fulllv', title: '满载率', style:'font-weight: 600;'},
            {field: 'dmoney', title: '每单运费', style:'font-weight: 600;'},

            {field: 'dcsj', title: '到场时间', style:'font-weight: 600;', style:'font-weight: 600;background-color: #dbf1e1;'},
            {field: 'dczxly', title: '执行来源', style:'font-weight: 600;', style:'font-weight: 600;background-color: #dbf1e1;'},
            {field: 'dcsfyz', title: '是否与提货地址一致', style:'font-weight: 600;', style:'font-weight: 600;background-color: #dbf1e1;'},

            {field: 'lcsj', title: '离场时间', style:'font-weight: 600;'},
            {field: 'lczxly', title: '执行来源', style:'font-weight: 600;'},

            {field: 'ddsj', title: '到达时间', style:'font-weight: 600;background-color: #dbf1e1;'},
            {field: 'ddzxly', title: '执行来源', style:'font-weight: 600;background-color: #dbf1e1;'},
            {field: 'ddsfyz', title: '是否与送货地址一致', style:'font-weight: 600;background-color: #dbf1e1;'},

            {field: 'qssj', title: '签收时间'},
            {field: 'qszxly', title: '执行来源'},
            {field: 'sfyz', title: '是否与送货地址一致'},

            {field: 'hdsj', title: '回单时间'},

            {field: 'yssx', title: '运输时效', style:'font-weight: 600;'},
            {field: 'pssx', title: '配送时效', style:'font-weight: 600;'},
            {field: 'lcwz', title: '流程完整', style:'font-weight: 600;',
            	templet: function(d){
					var lcwz = '否'
					if(d.dcsj !='' && d.dcsj != null){
						if(d.lcsj !='' && d.lcsj != null){
							if(d.ddsj !='' && d.ddsj != null){
								if(d.qssj !='' && d.qssj != null){
	            					lcwz =  '是'
								} 
							} 
						} 
					} 
        			return lcwz
            	}
			},
            {field: 'lcwz', title: '司机执行是否异常', style:'font-weight: 600;',
            	templet: function(d){
					if(d.dczxly == '司机' && d.ddzxly == '司机'){
						return '正常'
					} else {
        				return '异常'
					}
            	}
			},
        ]];
    };

    /**
     * 导出excel按钮
     */
    TDispatch.exportExcel = function () {
        var checkRows = table.checkStatus(TDispatch.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
            queryData['operateId'] = $("#operateId").val();
            queryData['operateName'] = $("#operateName").val();
            queryData['driver1'] = $("#driver1").val();
            queryData['carnumbger'] = $("#carnumbger").val();
            queryData['getUnit'] = $("#getUnit").val();
            queryData['condition'] = $("#condition").val();
            queryData['startTime'] = $("#startTime").val();
            queryData['endTime'] = $("#endTime").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/tDispatch/findPeisongfenxiAll",
                dataType: 'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    // 导出excel
    $('#btnExp').click(function () {
        TDispatch.exportExcel();
    });

    /**
     * 点击查询按钮
     */
    TDispatch.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['operateName'] = $("#operateName").val();
        queryData['driver1'] = $("#driver1").val();
        queryData['carnumbger'] = $("#carnumbger").val();
        queryData['getUnit'] = $("#getUnit").val();
        queryData['condition'] = $("#condition").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        table.reload(TDispatch.tableId, {
            where: queryData, page: {curr: 1},
			done: function (res, curr, count) {
			}
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TDispatch.tableId,
        url: Feng.ctxPath + '/tDispatch/findPeisongfenxiPage',
        page: true,
        limit: 60,
        limits:[60,120,240,360,480,1500,5000],
        height: "full-60",
        totalRow: true,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print'],
        cellMinWidth: 100,
        cols: TDispatch.initColumn(),
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TDispatch.search();
    });

    // 单击行事件
    table.on('row(' + TDispatch.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + TDispatch.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + TDispatch.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });
    
});
